<?php

class Admin_Model_ForumCategory extends Zend_Db_Table_Abstract
{

    protected $_name = 'forum_categories';
    
    /**
     * 创建分区
     *
     * @param string $catname
     */
    public function createCategory($catname){
        $row = $this->createRow();
        $row->catname = $catname;
        $row->position = $this->_getLastPosition() +1;
        $row->save();
        
        if ($row){
            return $row->id;
        }
        else{
            return null;
        }
    }
    
    /**
     * 更新分区
     * 
     * @param int $id
     * @param string $catName
     */
    public function updateCategory($id, $catname)
    {
        $row = $this->find($id)->current();
        if ($row){
            $row->catname = $catname;
            $row->save();
            return true;
        }
        else{
            return false;
        }
    }
    
    /**
     * 删除分区
     *
     * @param int $id
     * @throws Zend_Exception
     */
    public function deleteCategory($id){
        
        $row = $this->find($id)->current();
        if($row){
            $row->delete();
            return true;
        } else {
            throw new Zend_Exception('删除版块分区失败');
        }
    }
    
    /**
     * 获取位置
     *
     * @param int $id
     */
    private function _getLastPosition($id = null)
    {
        $select = $this->select();
        $select->order('position DESC');
        $row = $this->fetchRow($select);
        if($row){
            return $row->position;
        }
        else{
            return 0;
        }
    }
    
    /**
     * 向上移动位置
     * 
     * @param int $id
     * @throws Zend_Exception
     */
    public function moveUp($id)
    {
        $row = $this->find($id)->current();
        if($row){
            $position = $row->position;
            if($position < 1){ //　位置已处于最顶端
                return false; 
            }
            else{
                $select = $this->select();
                $select->where("position < ?", $position);
                $select->order("position DESC");
                $downItem = $this->fetchRow($select);
                if($downItem){
                    $downPosition = $downItem->position;
                    $downItem->position = $position;
                    $downItem->save();
                    $row->position = $downPosition;
                    $row->save();
                }
            }
        }
        else{
            throw new Zend_Exception("菜单项不存在！");
        }
    }

    /**
     * 向下移动位置
     * 
     * @param int $id
     * @throws Zend_Exception
     */
    public function moveDown($id)
    {
        $row = $this->find($id)->current();
        if($row){
            $position = $row->position;
            if($position == $this->_getLastPosition()){ // 位置已处于最底端
                return false; 
            }
            else{
                $select = $this->select();
                $select->where("position > ?", $position);
                $select->order("position ASC");
                $upItem = $this->fetchRow($select);
                if($upItem){
                    $upPosition = $upItem->position;
                    $upItem->position = $position;
                    $upItem->save();
                    $row->position = $upPosition;
                    $row->save();
                }
            }
        }
        else{
            throw new Zend_Exception("菜单项不存在！");
        }
    }
    
}

